package com.penghe.www.baselibrary.ui.fragments

import android.os.Bundle
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


/**
 * Created by alm on 17-6-1.
 *
 * fragment的父类
 */

abstract class BaseFragment : Fragment() {

    private var root: View? = null
    /**
     * Called to have the fragment instantiate its user interface view.
     * This is optional, and non-graphical fragments can return null (which
     * is the default implementation).  This will be called between
     * [.onCreate] and [.onActivityCreated].

     *
     * If you return a View from here, you will later be called in
     * [.onDestroyView] when the view is being released.

     * @param inflater The LayoutInflater object that can be used to inflate
     * * any views in the fragment,
     * *
     * @param container If non-null, this is the parent view that the fragment's
     * * UI should be attached to.  The fragment should not add the view itself,
     * * but this can be used to generate the LayoutParams of the view.
     * *
     * @param savedInstanceState If non-null, this fragment is being re-constructed
     * * from a previous saved state as given here.
     * *
     * *
     * @return Return the View for the fragment's UI, or null.
     */
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        root = inflater.inflate(setLayout(), container, false)
        return root!!
    }

    /**
     * Called when the fragment's activity has been created and this
     * fragment's view hierarchy instantiated.  It can be used to do final
     * initialization once these pieces are in place, such as retrieving
     * views or restoring state.  It is also useful for fragments that use
     * [.setRetainInstance] to retain their instance,
     * as this callback tells the fragment when it is fully associated with
     * the new activity instance.  This is called after [.onCreateView]
     * and before [.onViewStateRestored].

     * @param savedInstanceState If the fragment is being re-created from
     * * a previous saved state, this is the state.
     */
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initData()
        initViews()
        initViewData()
        initEvents()
    }

    /**
     * 获取fragment的布局

     * @return 布局id
     */
    protected abstract fun setLayout(): Int

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    /**
     * 初始化控件
     */
    protected abstract fun initViews()

    /**
     * 初始化控件数据
     */
    protected abstract fun initViewData()

    /**
     * 初始化事件
     */
    protected abstract fun initEvents()

    /**
     * 让fragment可以和Activity一样拥有findViewById函数
     */
    protected fun findViewById(@IdRes id: Int): View {
        return root!!.findViewById(id)
    }
}
